package com.example.accessibilitydemo.service;

import com.example.accessibilitydemo.util.PreferenceUtil;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

/**
 * 微信登录
 * 
 * @author chencq
 * 
 *         流程： 我-->设置-->退出-->退出当前帐号-->退出
 */
public class WeChatLogoutService extends BaseAccessibilityService {

	private static final String TAG = WeChatLogoutService.class.getSimpleName();

	private boolean logout = false;

	@Override
	public void onAccessibilityEvent(AccessibilityEvent event) {
		super.onAccessibilityEvent(event);

		logout = PreferenceUtil.getBoolean("logout", false);
		int eventType = event.getEventType();

		if (!logout)
			return;

		if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
				&& event.getClassName().equals("com.tencent.mm.ui.LauncherUI")) {
			AccessibilityNodeInfo meTab = findView("com.tencent.mm:id/bq0", "我");
			performViewClick(meTab);

			sleep(1000);

			clickViewByText("设置");
		} else if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
				&& event.getClassName().equals("com.tencent.mm.plugin.setting.ui.setting.SettingsUI")) {
			delayClickViewByText("退出", 1000);
		} else if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
				&& event.getClassName().equals("com.tencent.mm.ui.base.h")) {
			// 退出当前帐号
			AccessibilityNodeInfo logoutAccout = findViewByID("com.tencent.mm:id/bch");
			// 退出
			AccessibilityNodeInfo confirm = findViewByID("com.tencent.mm:id/abg");

			if (logoutAccout != null) {
				performViewClick(logoutAccout);
				sleep(1000);
			} else if (confirm != null) {
				performViewClick(confirm);
			}
		} else if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
				&& event.getClassName().equals("com.tencent.mm.ui.account.LoginHistoryUI")) {
			Log.i(TAG, "微信退出成功");
			PreferenceUtil.commitBoolean("logout", false);
			performHomeClick();
		}
	}
}
